package ch04;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author yang
 * @date 2024-04-09
 */
public class TestMain {
    public static void main(String[] args) {
        //1.初始化学生类数组
        Student[] stuArrays = {
                new Student("张三", 28),
                new Student("李四", 20),
                new Student("王五", 23)
        };
        //2.调用排序方法
        new TestMain().arraysStudent(stuArrays);
        //3.输出排序后的学生信息
        for (Student student : stuArrays) {
            System.out.println(student.getName()+"-"+student.getAge());
        }
    }

    /**
     * 降序排序学生信息的方法
     * @param students 需要降序排序的学生类数组
     */
    public void arraysStudent(Student[] students){
        Arrays.sort(students, new Comparator<Student>(){
            @Override
            public int compare(Student stu1, Student stu2) {
                return Integer.compare(stu2.getAge(), stu1.getAge());
            }
        });

    }
}
